package com.mscsoft.brainpower.server.service;

import java.util.HashMap;
import java.util.Random;
import javax.annotation.Resource;

import com.mscsoft.brainpower.server.dao.*;
import com.mscsoft.brainpower.server.domain.*;

import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserService {
    Random random = new Random(System.currentTimeMillis());
    HashMap<String, String> checkCodes = new HashMap<>();
    @Resource
    UserDao userDao;
    @Resource
    MailService mailService;

    // 注册
    public Msg<Object> register(String account, String password, String checkCode) {
        if (account == null || password == null) {
            return new Msg<>(400, Msg.s400);
        }
        User user = userDao.queryUserByAccount(account);
        if (user != null) {
            return new Msg<>(409, "账号已存在");
        }
        if (checkCodes.containsKey(account) && checkCodes.get(account).equals(checkCode)) {
            checkCodes.remove(account);
            userDao.insertNewAccount(account, password);
            return new Msg<>(200, "注册成功");
        } else {
            return new Msg<>(406, "验证码错误");
        }
    }

    // 登录
    String accountNotExist = "账号不存在";

    public Msg<Object> login(String account, String password) {
        if (account == null || password == null) {
            return new Msg<>(400, Msg.s400);
        }
        User user = userDao.queryUserByAccount(account);
        if (user == null) {
            return new Msg<>(409, accountNotExist);
        } else if (!password.equals(user.getPassword())) {
            return new Msg<>(409, "密码错误");
        } else {
            return new Msg<>(200, "登录成功", user.getUid());
        }
    }

    public Msg<Object> nicknameUpdate(String account, String password, String nickname) {
        if (account == null || password == null || nickname == null) {
            return new Msg<>(400, Msg.s400);
        }
        User user = userDao.queryUserByAccount(account);
        if (user == null) {
            return new Msg<>(409, accountNotExist);
        } else if (!password.equals(user.getPassword())) {
            return new Msg<>(409, "密码错误");
        } else {
            userDao.updateNickname(account, nickname);
            return new Msg<>(200, "昵称修改成功");
        }
    }

    public Msg<Object> pswUpdate(String account, String oldPassword, String newPassword) {
        if (account == null || oldPassword == null || newPassword == null) {
            return new Msg<>(400, Msg.s400);
        }
        User user = userDao.queryUserByAccount(account);
        if (user == null) {
            return new Msg<>(409, accountNotExist);
        } else if (!oldPassword.equals(user.getPassword())) {
            return new Msg<>(406, "原密码错误");
        } else {
            userDao.updatePassword(account, newPassword);
            return new Msg<>(200, "密码修改成功");
        }
    }

    public Msg<Object> findPsw(String account, String checkCode, String newPassword) {
        if (account == null || checkCode == null || newPassword == null) {
            return new Msg<>(400, Msg.s400);
        }
        User user = userDao.queryUserByAccount(account);
        if (user == null) {
            return new Msg<>(409, accountNotExist);
        }
        if (checkCodes.containsKey(account) && checkCodes.get(account).equals(checkCode)) {
            checkCodes.remove(account);
            userDao.updatePassword(account, newPassword);
            return new Msg<>(200, "密码修改成功");
        } else {
            return new Msg<>(406, "验证码错误");
        }
    }

    /** 为account创建一个6位验证码 */
    public String creCheckCode(String account) {
        String checkCode = String.format("%06d", random.nextInt(1000000));
        checkCodes.put(account, checkCode);
        return checkCode;
    }

    public Msg<Object> sendCheckCode(String mail) {
        String str = creCheckCode(mail);
        return mailService.sendMail(mail, "[BrianPower]帐号邮件验证码",
                "您好!\t验证码: " + str + ", 用于注册BrianPower帐号" + mail + "。如非您操作请忽略。");
    }
}
